package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	counts := make(map[string]int)
	if len(os.Args[1:]) == 0 {
		countLine(os.Stdin, counts)

	} else {
		for _, arg := range os.Args[1:] {
			f, err := os.Open(arg)
			if err != nil {
				fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
				continue
			}
			countLine(f, counts)
			f.Close()
		}
	}

	for msg, count := range counts {
		fmt.Printf("msg:%s, count:%d\n", msg, count)
	}

	fmt.Println()
	fmt.Println()
	fmt.Println()
	fmt.Println()
	fmt.Println()
	for _, arg := range os.Args[1:] {
		f, _ := os.Open(arg)
		fmt.Println(f)
	}
}

func countLine(file *os.File, counts map[string]int) {
	input := bufio.NewScanner(file)
	for input.Scan() {
		newline := input.Text()
		if counts[newline] > 0 {
			fmt.Println(file.Name())
		}
		counts[newline]++
	}
}
